package cn.com.self.apis.member.controller;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.member.entity.*;
import cn.com.self.apis.member.entity.vo.MemberInfoVO;
import cn.com.self.apis.member.service.IAccountService;
import cn.com.self.apis.member.service.IMemberInfoRelationService;
import cn.com.self.apis.member.service.IMemberInfoService;
import cn.com.self.apis.member.service.IMemberService;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangwt
 * @since 2021-01-10
 */
@Controller
@RequestMapping("/memberInfo")
@Api(tags = "会员信息类")
public class MemberInfoController {
    @Autowired
    private IMemberInfoService memberInfoService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IMemberInfoRelationService memberRelationService;
//    /**新增托管
//     * @Description //TODO
//     * @return
//     **/
//    @RequestMapping(value = "/create", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "新增会员信息")
//    @Transactional
//    public ResponseEntity create(@RequestBody @Valid MemberInfoVO entity, HttpServletRequest request){
//        UMember member = new UMember();
//        member.setPassword(entity.getPassword());
//        member.setPhone(entity.getPhone());
//        UMember memberCreate = memberService.create(member);
//        entity.setMemberId(memberCreate.getId());
//        UMemberInfo uMemberInfo = new UMemberInfo();
//        CopyUtils.copyProperties(entity, uMemberInfo);
//        uMemberInfo.setCreateDate(new Date());
//        uMemberInfo.setUpdateDate(new Date());
//
//        UMemberInfo create = memberInfoService.create(uMemberInfo);
//        UAccountInfo accountInfo = new UAccountInfo();
//        accountInfo.setMemberId(memberCreate.getId());
//        accountService.create(accountInfo);
//        UMemberInfoRelation memberInfoRelation = new UMemberInfoRelation();
//        memberInfoRelation.setMemberInfoId(memberCreate.getId());
//        memberInfoRelation.setType(entity.getType());
//        memberInfoRelation.setVal(entity.getVal());
//        memberRelationService.create(memberInfoRelation);
//        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
//    }
    /**新增会员信息
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增会员信息")
    @Transactional
    public ResponseEntity create(@RequestBody @Valid MemberInfoVO entity, HttpServletRequest request){
        UMember member = new UMember();
        member.setPassword(entity.getPassword());
        member.setPhone(entity.getPhone());
        UMember memberCreate = memberService.create(member);
        entity.setMemberId(memberCreate.getId());
        UMemberInfo uMemberInfo = new UMemberInfo();
        CopyUtils.copyProperties(entity, uMemberInfo);
        uMemberInfo.setCreateDate(new Date());
        uMemberInfo.setUpdateDate(new Date());
        uMemberInfo.setGrading("5");

        UMemberInfo create = memberInfoService.create(uMemberInfo);
        UAccountInfo accountInfo = new UAccountInfo();
        accountInfo.setMemberId(create.getId());
        accountService.create(accountInfo);
        if(entity.getMemberInfoRelations() != null && entity.getMemberInfoRelations().size() > 0){
            List<UMemberInfoRelation> memberInfoRelations = entity.getMemberInfoRelations();
            for(int i=0;i<memberInfoRelations.size();i++){
                UMemberInfoRelation memberInfoRelation = new UMemberInfoRelation();
                memberInfoRelation.setMemberInfoId(create.getId());
                memberInfoRelation.setType(memberInfoRelations.get(i).getType());
                memberInfoRelation.setVal(memberInfoRelations.get(i).getVal());
                memberRelationService.create(memberInfoRelation);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }


    /**分页获取会员列表
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取会员列表")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "realName", value = "姓名", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "昵称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "telephone", value = "电话", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "page", value = "页码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "size", value = "一页显示条数", required = false, dataType = "String")
    })
    public ResponseEntity getEscrowInfoPage(HttpServletRequest request, Pageable page){
        //Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        //QueryWrapper<MemberInfoVO> wrapper = new QueryWrapper<>();// 构建一个查询的wrapper
        //wrapper.eq("delFlag",0);
        //memberInfoService.page()
        //ICustomPage<UMemberInfo> pageData = this.memberInfoService.page(queryParams, page);
        //return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataPage(pageData)));
        return this.memberInfoService.getMemberInfoPage(request,page);

    }

    /**获取会员信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取会员信息")
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowInfo(@PathVariable("id") String id, HttpServletRequest request) {
        UMemberInfo memberInfo = this.memberInfoService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(memberInfo)));

    }


    /**修改会员信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改会员信息")
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody MemberInfoVO entity, HttpServletRequest request) {
        UMemberInfo memberInfo = this.memberInfoService.load(id);
        CopyUtils.copyProperties(entity, memberInfo);
        memberInfo.setUpdateDate(new Date());
        UMemberInfo update = this.memberInfoService.update(memberInfo);
        UMember member = this.memberService.load(update.getMemberId().toString());
        if (StringUtils.isNoneEmpty(entity.getPhone())){
            member.setPhone(entity.getPhone());
            UMember memberUpdate = this.memberService.update(member);
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


    /**删除会员信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除会员信息")
    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable("id") String id){
        return this.memberInfoService.deleteInfo(id);

    }


    /**修改账号信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改账号信息")
    @RequestMapping(value = {"/updateAccount/{memberId}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity updateAccount(@PathVariable("memberId") String memberId, @RequestBody UAccountInfo entity, HttpServletRequest request) {
        UAccountInfo accountInfo = this.accountService.queryAccountByMemberId(memberId);
        CopyUtils.copyProperties(entity, accountInfo);
        entity.setId(accountInfo.getId());
        UAccountInfo update = this.accountService.update(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


}
